package algorithm_demo.leetcode;

/**
 * 7. 整数反转
 * <a href="https://leetcode.cn/problems/reverse-integer/">https://leetcode.cn/problems/reverse-integer/</a>
 * @author Api
 * @date 2023/4/23 23:11
 */
public class Problem_007_ReverseInteger {
    public static int reverse(int x) {
        int result = 0;
        while(x != 0){
            int m = x % 10;
            int newInt = result * 10 + m;
            if ((newInt - m) / 10 != result){
                //超过Integer的最大值，则返回为0
                return 0;
            }
            x = x / 10;
            result = newInt;
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        int reverse = reverse(-1534236469);
        System.out.println(reverse);
    }
}
